package com.cloud.wms.ui.in.presnter;


import android.text.TextUtils;

import com.cloud.core.base.BaseRxPresenter;
import com.cloud.core.http.error.ApiException;
import com.cloud.core.utils.MessageCreator;
import com.cloud.core.utils.TimeUtils;
import com.cloud.wms.app.FineExApplication;
import com.cloud.wms.config.HttpConfig;
import com.cloud.wms.http.Params;
import com.cloud.wms.http.observer.LoadingObserver;
import com.cloud.wms.http.observer.ResponseWrapper;
import com.cloud.wms.tools.MemberFun;
import com.cloud.wms.tools.PageHelper;
import com.cloud.wms.tools.RxUtils;
import com.cloud.wms.tools.TaskHelper;
import com.cloud.wms.ui.in.contract.InRegisterContract;
import com.cloud.wms.ui.in.entity.InOrderEntity;
import com.cloud.wms.ui.main.entity.RemoteEntity;
import com.cloud.wms.ui.main.entity.TaskEntity;
import com.cloud.wms.ui.main.entity.TitleEntity;
import com.cloud.wms.ui.main.entity.UserEntity;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;

/**
 * @Author:Chen
 * @Date: 2021-01-14 15:51
 * @Description:
 */
public class InRegisterPresenter extends BaseRxPresenter<InRegisterContract.View> implements InRegisterContract.Presenter {
    public static final int IN_TASK = 0x104;
    public static final int IN_ORDER = 0x102;
    public static final int IN_REGISTER_SUBMIT = 0x101;

    @Inject
    public InRegisterPresenter() {

    }


    @Override
    public void loadInOrderList(String inCode, int page) {
        Params params = Params.newInstance(Params.Type.MEMBER);
        if (!TextUtils.isEmpty(inCode))
            params.put("InCode", inCode);
        params.put("InStatus", "1");
        FineExApplication
                .component()
                .http()
                .loadInOrderList(params)
                .map(new ResponseWrapper<>())
                .map(new MemberFun<InOrderEntity>())
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<ArrayList<TitleEntity<InOrderEntity>>>(mView.provideContext()) {
                    @Override
                    public void onSuccess(ArrayList<TitleEntity<InOrderEntity>> info) {
                        if (page == 0) {
                            mView.onSuccess(MessageCreator.createMessage(info, PageHelper.LIST_NEW_DATA));
                        } else {
                            mView.onSuccess(MessageCreator.createMessage(info, PageHelper.LIST_NEXT_DATA));
                        }
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                        if (TextUtils.isEmpty(inCode))
                            mView.showError();
                    }
                });
    }

    @Override
    public void receiveRegisterTask(InOrderEntity item, String taskCode) {
        TaskHelper.receiveTask(item.getInID() + "", taskCode)
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<TaskEntity>(mView.provideContext()) {
                    @Override
                    public void onSuccess(TaskEntity info) {
                        mView.onSuccess(MessageCreator.createMessage(info, IN_TASK));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });
    }

    /**
     * 到货登记提交X
     *
     * @param taskEntity
     * @param inOrder
     * @param plate
     * @param volume
     * @param boxAmount
     * @param date
     * @param isAll
     * @param remark
     * @param images
     */
    @Override
    public void submitRegisterTask(TaskEntity taskEntity, InOrderEntity inOrder, String plate, String volume, String boxAmount, String date,
                                   boolean isAll, String remark, List<String> images, String beginTime) {
        Params params = Params.newInstance(Params.Type.MEMBER);
        params.put("licensePlate", plate);
        params.put("areaAmount", volume);
        params.put("boxAmount", boxAmount);
        params.put("arrivalDate", date + " 00:00:00");
        params.put("isReceipt", isAll ? "2" : "1");
        params.put("arrivalMemo", remark);
        // TODO: 2021/1/26 上传文件
        params.put("phoneUrl", "");
        params.put("userID", FineExApplication.component().loginCache().getUserInfo().getUserID());
        params.put("beginTime", beginTime);
        params.put("completeTime", TimeUtils.getCurrentDateAndTime());

        if (inOrder != null) {
            params.put("inID", inOrder.getInID());
            params.put("warehouseAreaID", inOrder.getWarehouseAreaID());
        }

        Observable<Object> observable;
        if (taskEntity == null) {
            params.put("workID", "0");
            params.put("appKey", FineExApplication.component().loginCache().getAppKey());
            observable = FineExApplication
                    .component()
                    .http()
                    .submitRegister(params)
                    .map(new ResponseWrapper<>());
        } else {
            params.put("inID", taskEntity.getMainBillID());
            params.put("inCode", taskEntity.getMainBillCode());
            params.put("workID", taskEntity.getWorkID());
            params.put("productCategory", taskEntity.getProductCategory());
            params.put("jobStationID", taskEntity.getJobStationID());
            params.put("appKey", taskEntity.getAppKey());
            params.put("warehouseAreaID", taskEntity.getWarehouseAreaID());
            observable = FineExApplication
                    .component()
                    .http()
                    .submitRegisterTask(params)
                    .map(new ResponseWrapper<>());
        }

        observable
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<Object>(mView.provideContext()) {
                    @Override
                    public void onSuccess(Object info) {
                        mView.onSuccess(MessageCreator.createMessage(info, IN_REGISTER_SUBMIT));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });
    }
}
